Chrono-TZ 0.5.3
Chrono-TZ
is a library that provides implementors of the
TimeZone
trait for rust-chrono
. The
impls are generated by a build script using the IANA database
and parse-zoneinfo
.
Documentation
Documentation is hosted on docs.rs
Usage
Put this in your Cargo.toml
:
[]
= "0.4"
= "0.5"
Then you will need to write (in your crate root):
extern crate chrono;
extern crate chrono_tz;
Examples
Create a time in one timezone and convert it to UTC
use ;
use US Pacific;
let pacific_time = Pacific.ymd.and_hms;
let utc_time = pacific_time.with_timezone;
assert_eq!;
Create a naive datetime and convert it to a timezone-aware datetime
use ;
use Johannesburg;
let naive_dt = from_ymd.and_hms;
let tz_aware = Johannesburg.from_local_datetime.unwrap;
assert_eq!;
London and New York change their clocks on different days in March so only have a 4-hour difference on certain days.
use TimeZone;
use London;
use New_York;
let london_time = London.ymd.and_hms;
let ny_time = london_time.with_timezone;
assert_eq!;
You can get the raw offsets as well if you want to see the standard
UTC offset as well as any special offsets in effect (such as DST)
at a given time. Note that you need to import the OffsetComponents
trait.
use ;
use London;
use OffsetComponents;
let london_time = London.ymd.and_hms;
// London typically has zero offset from UTC, but has a 1h adjustment forward
// when summer time is in effect.
assert_eq!;
assert_eq!;
Adding 24 hours across a daylight savings change causes a change in local time
use ;
use London;
let dt = London.ymd.and_hms;
let later = dt + hours;
assert_eq!;
And of course you can always convert a local time to a unix timestamp
use TimeZone;
use Kolkata;
let dt = Kolkata.ymd.and_hms;
let timestamp = dt.timestamp;
assert_eq!;
Pretty-printing a string will use the correct abbreviation for the timezone
use TimeZone;
use London;
let dt = London.ymd.and_hms;
assert_eq!;
assert_eq!;
You can convert a timezone string to a timezone using the FromStr trait
use TimeZone;
use Tz;
use UTC;
let tz: Tz = "Antarctica/South_Pole".parse.unwrap;
let dt = tz.ymd.and_hms;
let utc = dt.with_timezone;
assert_eq!;
no_std
Support
To use this library without depending on the Rust standard library, put this
in your Cargo.toml
:
[]
= { = "0.4", = false }
= { = "0.5", = false }
If you are using this library in an environment with limited program space, such as a microcontroller, take note that you will also likely need to enable optimizations and Link Time Optimization:
[]
= 2
= true
[]
= true
Otherwise, the additional binary size added by this library may overflow available program space and trigger a linker error.
Future Improvements
- Handle leap seconds
- Handle Julian to Gregorian calendar transitions
- Load tzdata always from latest version
- Dynamic tzdata loading